/**
 * CourseDetailsViewService.java
 * com.sftz.modules.minapp.coursedetails.service
 * Copyright (c) 2018, 北京科技有限公司版权所有.
*/

package com.sftz.modules.minapp.coursestudy.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.google.common.base.Objects;
import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.CourseLearningRecordEntity;
import com.sftz.assets.enums.ResourcesSortEnum;
import com.sftz.assets.enums.StudyStateEnum;
import com.sftz.framework.service.aliyun.AliyunVodUtil;
import com.sftz.framework.service.qiniu.QiniuDownloadService;
import com.sftz.framework.util.StringUtil;
import com.sftz.modules.minapp.coursestudy.dto.CourseLearningRecordDto;
import com.sftz.modules.minapp.coursestudy.dto.InsCourseStructureDto;
import com.sftz.modules.minapp.coursestudy.form.CourseRecordForm;
import com.uxuexi.core.common.util.EnumUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

@IocBean
public class CoursegRecordService {

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	@Inject
	private QiniuDownloadService qiniuDownloadService;

	/**
	 * 存储播放记录
	 * @Date 2018年12月18日
	 * @author gyp
	 * @param sqlParamForm
	 * @param userId
	 * @throws Exception(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	@Aop("txDb")
	public CourseLearningRecordEntity insterRecourd(final CourseRecordForm form) throws Exception {

		CourseLearningRecordEntity record = addRecourd(form);
		record = dbDao.insert(record);
		return record;
	}

	/**
	 * 获取课程信息
	 * @Date 2018年12月28日
	 * @author gyp
	 * @param courseid
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public CourseEntity courseInfo(CourseRecordForm form) {
		if (!Util.isEmpty(form)) {
			CourseEntity course = dbDao.fetch(CourseEntity.class, Cnd.where("id", "=", form.getCourseid()));
			return course;
		}
		return null;
	}

	/**
	 * 存储学习记录信息
	 * @Date 2018年12月18日
	 * @author gyp
	 * @param sqlParamForm
	 * @param userId
	 * @return
	 * @throws Exception(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	private CourseLearningRecordEntity addRecourd(final CourseRecordForm form) throws Exception {
		CourseLearningRecordEntity record = new CourseLearningRecordEntity();
		record.setUserId(form.getUserid());
		//封装学习 状态 学习 进度
		record.setLearningProgress(0);
		//因为是第一次学习 学进度 给为 学习中 STUDYCENTRE
		record.setLearningState(StudyStateEnum.STUDYCENTRE.intKey());
		record.setSectionId(form.getStructureId());
		//获取章节所需要的相关信息
		Sql sql = Sqls.create(sqlManager.get("minapp_courseStudy_findResource"));
		sql.params().set("structureId", form.getStructureId());
		InsCourseStructureDto resource = DbSqlUtil.fetchEntity(dbDao, InsCourseStructureDto.class, sql);
		record.setSectionName(resource.getResourceName());
		record.setCourseId(resource.getCourseId());
		//封装 学习时间 和 课程分类
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df.parse(df.format(new Date()));
		record.setLearningTime(date);
		record.setResourceType(resource.getResourceType());
		record.setResourceId(resource.getResourceId());
		record.setResourceName(resource.getResourceName());
		record.setUrl(resource.getResourceUrl());
		return record;
	}

	/**
	 * 获取本节课程下最新的学习记录
	 * @Date 2018年5月24日
	 * @author gyp
	 * @param courseId
	 * @param userId
	 */
	public CourseLearningRecordDto getLearningRecordFirst(CourseRecordForm recordForm) {

		Sql sql = Sqls.create(sqlManager.get("minapp_course_learning_record"));
		sql.setParam("courseId", recordForm.getCourseid());
		sql.setParam("userId", recordForm.getUserid());
		CourseLearningRecordDto dto = DbSqlUtil.fetchEntity(dbDao, CourseLearningRecordDto.class, sql);
		if (!Util.isEmpty(dto)) {
			String playPath = getPlayUrl(dto);
			dto.setPlayPath(playPath);
		}
		return dto;

	}

	private String getPlayUrl(CourseLearningRecordDto resource) {
		String play_path = null;
		ResourcesSortEnum ct = EnumUtil.get(ResourcesSortEnum.class, resource.getResourceType());
		switch (ct) {
		case VIDEO://处理视频相关
			//获取视频连接的地址
			GetPlayInfoResponse response = AliyunVodUtil.getVideoInfos(resource.getUrl());
			List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
			// 播放地址
			for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
				if (Objects.equal("mp4", StringUtil.getSuffix(playInfo.getPlayURL()))) {
					play_path = playInfo.getPlayURL();
					break;
				}
			}
			break;
		case AUDIO:
			//音频相关
			//获取要播放的url
			//			InsResourceDataEntity insResourceData = dbDao.fetch(InsResourceDataEntity.class,
			//					Cnd.where("id", "=", resource.getUrl()));
			String playPath = "http://file.sktcm.cn/" + resource.getUrl();
			play_path = qiniuDownloadService.getDownloadUrl(playPath);
			break;
		case FILE: //文件
			play_path = resource.getUrl();
			break;
		case MIND: //导图
			play_path = resource.getUrl();
			break;
		case LINK: //链接
			play_path = resource.getUrl();
			break;
		case IMAGE_TEXT: //图文
			play_path = resource.getUrl();
			break;
		default:
			break;
		}
		return play_path;
	}
}
